package com.kj.tms.common.utils;


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;
import java.util.Map;

/**
 * @Author:zhuyongjie
 * @Description:Jackson json工具类
 * @Date:Create in  2017/10/17 15:39
 */
public class JsonUtils {
    private static ObjectMapper objectMapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);


    /**
     * JavaBean转换为json格式字符串
     * @param bean
     * @param <T>
     * @return
     */
    public static <T> String bean2Json(T bean){
        try {
            return objectMapper.writeValueAsString(bean);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Map转化为Json格式字符串
     * @param map
     * @return
     */
    public static String map2Json(Map map){
        try {
            return objectMapper.writeValueAsString(map);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T map2Bean(Map map,Class<T> beanClass){
        try {
            String json = map2Json(map);
            return json2Bean(json,beanClass);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * List转换为Json格式字符串
     * @param list
     * @return
     */
    public static String list2Json(List list){
        try {
            return objectMapper.writeValueAsString(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将Json格式字符串传传为JavaBean
     * @param json json格式字符串
     * @param beanClass Bean.class
     * @param <T>
     * @return
     */
    public static <T> T json2Bean(String json,Class<T> beanClass){
        try {
            //忽略不包含字段
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
            return objectMapper.readValue(json,beanClass);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * json格式字符传转化为List
     * @param json
     * @param beanClass
     * @param <T>
     * @return
     */
    public static <T> List<T> json2List(String json,Class<T> beanClass){
        try {
            return (List<T>)objectMapper.readValue(json,getCollectionType(List.class,beanClass));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json格式字符串转换为Map对象
     * @param json
     * @return
     */
    public static Map json2Map(String json){
        try {
            //空值自动转换
            objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY,false);
            return (Map) objectMapper.readValue(json,Map.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取集合类型
     * @param collectionClass
     * @param elementClasses
     * @return
     */
    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    /**
     * LinkHashMap 转bean( hushMap经过feign后会转为linkedHashMap,而linkhushmap转javabean会报错)
     *
     * @param object
     * @param beanClass
     * @param <T>
     * @return
     */
    public static <T> T Object2Bean(Object object, Class<T> beanClass) {
        try {
            T javaBean = objectMapper.convertValue(object, beanClass);
            return javaBean;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
